package com.ctjsoft.threadstatus;

/**
 * 测试守护线程
 *
 * @author liaoyy
 * @date Created in 2022/1/15 16:13
 */
public class TestDaemonThread {
    public static void main(String[] args) {
        DaemonThread daemonThread = new DaemonThread();
        Thread t1 = new Thread(daemonThread);
        t1.setDaemon(true);
        t1.start();

        UserThread userThread = new UserThread();
        Thread t2 = new Thread(userThread);
        t2.start();
    }
}

class DaemonThread implements Runnable{
    @Override
    public void run() {
        // important 虽然这里设置的运行条件是true，但是当用户线程结束后，JVM就会停止，因此守护线程也会随之结束
        while (true){
            System.out.println("this is a daemon thread");
        }
    }
}

class UserThread implements Runnable{
    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            System.out.println("---this is a user thread---" + i);
        }
        System.out.println("---user thread dead---");
    }
}